'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: router.py
* @Time: 2025/9/17
* @All Rights Reserve By Brtc
'''
from dataclasses import dataclass

from flask import Flask, Blueprint
from injector import inject
from internal.handler import (AppHandler,
                              BuiltinToolHandler,
                              ApiToolHandler,
                              UploaderHandler,
                              DatasetHandler,
                              SegmentHandler,
                              DocumentHandler,
                              OAuthHandler,
                              AccountHandler,
                              AuthHandler
                              )

@inject
@dataclass
class Router:
    app_handler:AppHandler
    builtin_tool_handler:BuiltinToolHandler
    api_tool_handler:ApiToolHandler
    upload_file_handler:UploaderHandler
    dataset_handler:DatasetHandler
    document_handler:DocumentHandler
    segment_handler:SegmentHandler
    oauth_handler:OAuthHandler
    account_handler:AccountHandler
    auth_handler:AuthHandler
    def register_router(self, app:Flask):
        """实例化一个蓝图"""
        blue_printer = Blueprint("llmops-ai-platform", __name__, url_prefix="")

        #添加url 路由表
        #App 相关的路由表
        blue_printer.add_url_rule("/ping", methods=["POST", "OPTIONS"],view_func= self.app_handler.ping)
        blue_printer.add_url_rule("/apps", methods=["POST"], view_func=self.app_handler.create_app)
        blue_printer.add_url_rule("/apps/<uuid:app_id>", view_func=self.app_handler.get_app)
        blue_printer.add_url_rule("/apps/<uuid:app_id>/draft-app-config",view_func=self.app_handler.get_draft_app_config)
        blue_printer.add_url_rule("/apps/<uuid:app_id>/draft-app-config", methods=["POST"], view_func=self.app_handler.update_draft_app_config, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/publish", methods=["POST"], view_func=self.app_handler.publish, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/cancel-publish", methods=["POST"],view_func=self.app_handler.cancel_publish, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/publish-histories",view_func=self.app_handler.get_publish_histories_with_page, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/fallback-history", methods=["POST"], view_func=self.app_handler.fallback_history_to_draft, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/summary", view_func=self.app_handler.get_debug_conversation_summary, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/summary", methods=["POST"], view_func=self.app_handler.update_debug_conversation_summary, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/conversations", methods=["POST"],view_func=self.app_handler.debug_chat, )
        blue_printer.add_url_rule("/apps/<uuid:app_id>/conversations/delete-debug-conversation", methods=["POST"],view_func=self.app_handler.delete_debug_conversation, )

        #工具相关接口
        blue_printer.add_url_rule("/builtin-tools/categories", view_func=self.builtin_tool_handler.get_categories)
        blue_printer.add_url_rule("/builtin-tools", view_func=self.builtin_tool_handler.get_builtin_tools)
        blue_printer.add_url_rule("/builtin-tools/<string:provider_name>/tools/<string:tool_name>", view_func=self.builtin_tool_handler.get_provider_tool)
        blue_printer.add_url_rule("/builtin-tools/<string:provider_name>/icon", view_func=self.builtin_tool_handler.get_provider_icon)

        blue_printer.add_url_rule("/api-tools/validate-openapi-schema", methods=["POST"], view_func=self.api_tool_handler.validate_openapi_schema)
        blue_printer.add_url_rule("/api-tools", methods=["POST"], view_func=self.api_tool_handler.create_api_tool_provider)
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>", view_func=self.api_tool_handler.get_api_tool_provider)
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>/tools/<string:tool_name>", view_func=self.api_tool_handler.get_api_tool)
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>/delete",methods=["POST"], view_func=self.api_tool_handler.delete_api_provider)
        blue_printer.add_url_rule("/api-tools",view_func=self.api_tool_handler.get_api_tool_providers_with_page)
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>", methods=["POST"],view_func=self.api_tool_handler.update_api_tool_provider)

        # 文件上传
        blue_printer.add_url_rule("/upload-files/file", methods=["POST"],view_func=self.upload_file_handler.upload_file)
        blue_printer.add_url_rule("/upload-files/image", methods=["POST"],view_func=self.upload_file_handler.upload_image)

        # 知识库模块
        blue_printer.add_url_rule("/datasets", view_func=self.dataset_handler.get_datasets_with_page)
        blue_printer.add_url_rule("/datasets", methods =["POST"],view_func=self.dataset_handler.create_dataset)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>", view_func=self.dataset_handler.get_dataset)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>", methods=["POST"], view_func=self.dataset_handler.update_dataset)
        blue_printer.add_url_rule("/datasets/embeddings", view_func=self.dataset_handler.embeddings_query)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents", methods=["POST"], view_func=self.document_handler.create_documents)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/batch/<string:batch>", view_func=self.document_handler.get_documents_status)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>", methods=["POST"], view_func=self.document_handler.update_document_enabled)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents", view_func=self.document_handler.get_documents_with_page,)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>", view_func=self.document_handler.get_document,)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/name",methods=["POST"], view_func=self.document_handler.update_document_name, )
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/delete", methods=["POST"], view_func=self.document_handler.delete_document)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/segments/<uuid:segment_id>",view_func=self.segment_handler.get_segment, )
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/segments/<uuid:segment_id>/enabled",methods=["POST"], view_func=self.segment_handler.update_segment_enabled, )
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/segments",view_func=self.segment_handler.get_segments_with_page, )
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/segments", methods=["POST"], view_func=self.segment_handler.create_segment, )
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/hit", methods=["POST"], view_func=self.dataset_handler.hit)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/queries",view_func=self.dataset_handler.get_data_queries)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/delete", methods=["POST"],view_func=self.dataset_handler.delete_dataset, )
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/segments/<uuid:segment_id>/delete",methods=["POST"], view_func=self.segment_handler.delete_segment, )
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/segments/<uuid:segment_id>", methods=["POST"], view_func=self.segment_handler.update_segment, )

        #授权认证模块
        blue_printer.add_url_rule("/oauth/<string:provider_name>", view_func=self.oauth_handler.provider)
        blue_printer.add_url_rule("/oauth/authorize/<string:provider_name>", methods=["POST"], view_func=self.oauth_handler.authorize)

        #账号模块
        blue_printer.add_url_rule("/account", view_func=self.account_handler.get_current_user)
        blue_printer.add_url_rule("/account/password", methods = ["POST"],view_func=self.account_handler.update_password)
        blue_printer.add_url_rule("/account/name", methods=["POST"], view_func=self.account_handler.update_name)
        blue_printer.add_url_rule("/account/avatar", methods=["POST"], view_func=self.account_handler.update_avatar)


        #登录模块
        blue_printer.add_url_rule("/auth/password-login", methods=["POST"], view_func=self.auth_handler.password_login)
        blue_printer.add_url_rule("/auth/logout", methods = ["POST"], view_func=self.auth_handler.logout)


        #将蓝图注册到 flask框架中去
        app.register_blueprint(blue_printer)